package com.jboltai.resource.functioncall;

import com.alibaba.fastjson.JSONObject;
import com.jboltai.capability.functioncall.FunctionParam;
import com.jboltai.resource.Resource;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class FunctionResource extends Resource implements Serializable{

    /**
     * 函数名称
     */
    private String name;

    /**
     * 函数描述
     */
    private String description;

    /**
     * 函数参数
     */
    private List<FunctionParam> parameters;

    /**
     * 必填参数名
     */
    private String[] required;


    public FunctionResource(String id, String name, String description) {
        super(id);
        this.name = name;
        this.description = description;
    }



    public String getName() {
        return name;
    }


    public String getDescription() {
        return description;
    }


    public List<FunctionParam> getParameters() {
        return parameters;
    }

    /**
     * 设置参数定义
     * @param parameters
     * @return
     */
    public FunctionResource setParams(List<FunctionParam> parameters) {
        this.parameters = parameters;
        processRequired();
        return this;
    }

    private void processRequired() {
        if(this.parameters!= null && this.parameters.size() > 0){
            this.required = this.parameters.stream().filter(FunctionParam::getRequired).map(FunctionParam::getName).toArray(String[]::new);
        }else{
            this.required = null;
        }
    }

    /**
     * 添加参数定义
     * @param parameter
     * @return
     */
    public FunctionResource addParameter(FunctionParam parameter){
        if(parameter == null){
            throw new RuntimeException("Function addParameter(parameter),parameter is null");
        }
        if(this.parameters == null){
            this.parameters = new ArrayList<>();
        }
        this.parameters.add(parameter);
        if (parameter.getRequired()) {
            processRequired();
        }
        return this;
    }


    public String[] getRequired() {
        return required;
    }

    public FunctionResource setRequired(String[] required) {
        this.required = required;
        return this;
    }

    /**
     * 将该函数转json对象
     * @return
     */
    public JSONObject toJSONObject() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", name);
        jsonObject.put("description", description);
        if(this.parameters !=null && this.parameters.size()>0){
            JSONObject parameters = new JSONObject();
            parameters.put("type","object");
            JSONObject properties = new JSONObject();
            for(FunctionParam parameter:this.parameters){
                properties.put(parameter.getName(), parameter.toJSONObject());
            }
            parameters.put("properties",properties);
            if(this.required!=null && this.required.length>0){
                parameters.put("required", this.required);
            }
            jsonObject.put("parameters", parameters);

        }
        return jsonObject;
    }

}
